package com.health.healthproject.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.healthproject.common.Result;
import com.health.healthproject.dto.HealthLogDTO;
import com.health.healthproject.entity.HealthLog;
import com.health.healthproject.entity.User;
import com.health.healthproject.mapper.HealthLogMapper;
import com.health.healthproject.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 健康日志管理接口
 */
@RestController
@RequestMapping("/health-log")
@RequiredArgsConstructor
public class HealthLogController {

    private final HealthLogMapper healthLogMapper;
    private final UserMapper userMapper;

    /**
     * 创建健康日志
     *
     * @param healthLog 健康日志信息
     * @return 创建成功的健康日志ID
     */
    @SaCheckRole("2")
    @PostMapping
    public Result<Long> create(@RequestBody @Validated HealthLog healthLog) {
        healthLogMapper.insert(healthLog);
        List<String> roleList = StpUtil.getRoleList();
        System.out.println(roleList.toString());
        return Result.success(healthLog.getId());
    }

    /**
     * 更新健康日志
     *
     * @param id 健康日志ID
     * @param healthLog 更新的健康日志信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody @Validated HealthLog healthLog) {
        healthLog.setId(id);
        healthLogMapper.updateById(healthLog);
        return Result.success();
    }

    /**
     * 删除健康日志
     *
     * @param id 健康日志ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        healthLogMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 获取健康日志详情
     *
     * @param id 健康日志ID
     * @return 健康日志详情
     */
    @GetMapping("/{id}")
    public Result<HealthLogDTO> getDetail(@PathVariable Long id) {
        HealthLogDTO detail = healthLogMapper.getHealthLogDetail(id);
        return Result.success(detail);
    }

    /**
     * 分页查询健康日志
     *
     * @param current 当前页码
     * @param size 每页大小
     * @param query 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<IPage<HealthLogDTO>> page(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            HealthLog query) {
        Page<HealthLog> page = new Page<>(current, size);
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        User user = userMapper.selectById(loginIdAsLong);
        if (StringUtils.isBlank(user.getUserAuth())){
            query.setUserId(loginIdAsLong);
        }
        IPage<HealthLogDTO> result = healthLogMapper.pageHealthLogList(page, query);
        return Result.success(result);
    }

    @GetMapping("/stats")
    public Result<List<HealthLog>> stats(){
        Long loginId = StpUtil.getLoginIdAsLong();
        User user = userMapper.selectById(loginId);
        if (null != user){
            Integer userType = user.getUserType();
            if (userType == 2){
                loginId = null;
            }
        }
        List<HealthLog> list = healthLogMapper.getStats(loginId);
        return Result.success(list);
    }
 }